package de.pharmacontrol.psa.beans.managed;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

import de.pharmacontrol.pilot.entities.User;
import de.pharmacontrol.psa.beans.interfaces.UserService;

@ManagedBean
@ApplicationScoped
public class UserManagedBean {

	@EJB
	UserService userBean;

	private String username;
	private String password;
	private String searchUser;
	private List<User> searchUsersResults;
	private User selectedUser;

	public UserManagedBean() {
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public User getSelectedUser() {
		if (selectedUser == null) {
			selectedUser = new User();
		}
		return selectedUser;
	}

	public void setSelectedUser(User selectedUser) {
		this.selectedUser = selectedUser;
	}

	public List<User> getSearchUsersResults() {
		return searchUsersResults;
	}

	public void setSearchUsersResults(List<User> searchUsersResults) {
		this.searchUsersResults = searchUsersResults;
	}

	public String getSearchUser() {
		return searchUser;
	}

	public void setSearchUser(String searchUser) {
		this.searchUser = searchUser;
	}

	public String login() {
		if (userBean.validateUser(getUsername(),getPassword())) {
			return "home";
		} else {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage("username", new FacesMessage("Invalid UserName and Password"));
			return "login";
		}
	}

	public void login(ActionEvent actionEvent) {
		RequestContext context = RequestContext.getCurrentInstance();
		FacesMessage msg = null;
		boolean loggedIn = false;
		
		if(userBean.validateUser(getUsername(),getPassword())) {
			loggedIn = true;
			msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", getUsername());
		} else {
			loggedIn = false;
			msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");
		}
		
		FacesContext.getCurrentInstance().addMessage(null, msg);
		context.addCallbackParam("loggedIn", loggedIn);
	}
	
	public String searchUser() {
		String username = (this.searchUser == null) ? "" : this.searchUser.trim();
		this.searchUsersResults = userBean.findUserByPattern(username);
		return "home";
	}

	public String updateUser() {
		userBean.updateUser(this.selectedUser);
		return "home";
	}

	public void onUserSelect(SelectEvent event) {
		selectedUser = (User) event.getObject();
	}

	public void onUserUnselect(UnselectEvent event) {
	}
}
